<template>
  <div class="follow-up">
    <main class="detail" v-if="detailData">
      <div class="inner">
        <div class="task-header">
          <div class="id">{{ $t("tag.task") }}ID：{{ detailData.task_no }}</div>
          <div class="status">
            {{ detailData.status_title }}
          </div>
        </div>
        <div class="divider-line"></div>
        <div class="title">
          {{ detailData.title }}
        </div>
        <div class="grid">
          <div class="detail-item">
            <div class="label">{{ $t("table.profile_task.col_2") }}：</div>
            <div class="content">
              {{ detailData.member_last_name + ' '+ detailData.member_first_name }}
            </div>
          </div>
          <div class="detail-item">
            <div class="label">{{ $t("content.task.receiver") }}：</div>
            <div class="content">
              {{
                detailData.receive_last_name +' ' + detailData.receive_first_name ||
                $t("tag.none")
              }}
            </div>
          </div>
          <div class="detail-item">
            <div class="label">
              {{ $t("dialog.profile_task.task_progress") }}：
            </div>
            <div class="time">{{ detailData.create_time }}</div>
          </div>
        </div>
        <div class="content raw-html">
          <div class="ql-editor" v-html="detailData.content"></div>
        </div>
      </div>
    </main>
    <div class="follow-records">
      <template v-if="detailData && followData">
        <div class="records-list">
          <div
            class="record-item"
            v-for="recordItem in followData"
            :key="recordItem.id"
          >
            <div class="title">
              <p>
                {{ $t("dialog.profile_task.task_progress") }}：{{
                  recordItem.create_time
                }}
              </p>
              <div>
                <div class="reciever-info">
                  <span class="label"
                    >{{
                      recordItem.member_id == detailData.member_id
                        ? $t("table.profile_task.col_2")
                        : $t("content.task.receiver")
                    }}：</span
                  >
                  <span class="user reciever">{{
                    recordItem.member_last_name +' ' + recordItem.member_first_name
                  }}</span>
                </div>
                <span class="replay-label"
                  >{{ $t("content.task.replay") }}：</span
                >
              </div>
            </div>
            <div class="record-card">
              <div class="card-title">
                <span
                  >{{ $t("form.task_fill.follow_form.item_1.label") }}：</span
                >
                <div class="raw-html">
                  <span class="ql-editor" v-html="recordItem.content"></span>
                </div>
              </div>
            </div>
            <div class="btns">
              <template
                v-if="
                  detailData.applicant_status == 0 ||
                  detailData.applicant_status == 3
                "
              >
                <a-button
                  type="primary"
                  danger
                  size="small"
                  ghost
                  @click="continueFollowUp"
                  v-if="
                    detailData.status != 4 &&
                    detailData.receive_id == memberStore.profile?.id
                  "
                  >{{ $t("btnText.continue_follow_task") }}</a-button
                >
                <a-button
                  type="primary"
                  size="small"
                  v-if="
                    detailData.status != 4 &&
                    detailData.receive_id == memberStore.profile?.id
                  "
                  @click="applyToStatement"
                  >{{ $t("btnText.apply_statements") }}</a-button
                >
                <a-button
                  type="primary"
                  size="small"
                  v-if="
                    detailData.status != 4 &&
                    detailData.member_id == memberStore.profile?.id
                  "
                  @click="replayRecord"
                  >{{ $t("btnText.replay") }}</a-button
                >
                <a-button
                  type="primary"
                  size="small"
                  @click="applyToStatement"
                  v-if="
                    detailData.status != 4 &&
                    detailData.member_id == memberStore.profile?.id
                  "
                  >{{ $t("btnText.to_statements") }}</a-button
                >
              </template>
            </div>
          </div>
        </div>
      </template>
      <template v-else> </template>
    </div>
  </div>
</template>

<script setup lang="ts">
import { message, Modal } from "ant-design-vue";
import { getCurrentInstance, h, onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import {
  postTaskDetail,
  postTaskFollowUpList,
  postTaskFollowUpToStatement,
} from "../../api/task";
import { useMemberStore } from "../../store";
const $t = getCurrentInstance()?.appContext.config.globalProperties.$t;

const memberStore = useMemberStore();

const route = useRoute();
const followData = ref();
const detailData = ref();
const task_id: unknown = route.query.task_id;
if (!task_id) {
  goto(-1);
}
postTaskDetail(task_id as number).then((res) => {
  detailData.value = res.data;
});
const getTaskFollowRecords = async () => {
  let res = await postTaskFollowUpList(task_id);
  if (res.code == 200) {
    followData.value = res.data;
  }
};

const replayRecord = () => {
  goto("/mobile/publishTask", { type: "replay", from: route.path });
};
const continueFollowUp = () => {
  goto("/mobile/publishTask", { type: "follow", from: route.path });
};

const applyToStatement = () => {
  let confirmModal = Modal.confirm({
    title: $t("dialog.task.ensure_apply_statement.title"),
    content: $t("dialog.task.ensure_apply_statement.content"),
    icon: () => h("", ""),
    async onOk() {
      confirmModal.update({
        okButtonProps: {
          loading: true,
        },
        maskClosable: false,
      });
      let res = await postTaskFollowUpToStatement(detailData.value.id);
      if (res.code == 200) {
        message.success($t("message.success.task.apply_statement"));
      } else {
        message.error($t("message.error.task.apply_statement"));
      }
      goto(-1);
      confirmModal.update({
        okButtonProps: {
          loading: false,
        },
        maskClosable: true,
      });
    },
    onCancel() {
    },
    class: "profile-confirm-modal",
    okText: $t("btnText.ensure"),
    cancelText: $t("btnText.cancel1"),
    width: "19.7917vw",
    maskClosable: true,
    centered: true,
  });
};
getTaskFollowRecords();
</script>
<style scoped lang="less">
.follow-up {
  padding: 82px;
  .detail {
    padding: 82px;
    background-color: #faf9f9;
    border-radius: 61px;
  }
  .follow-records {
    margin-top: 102px;
    font-size: 72px;
    .record-item {
      margin-top: 123px;
      .title {
        margin-bottom: 82px;
        & > div {
          margin-top: 24px;
          display: flex;
          flex-wrap: wrap;
          column-gap: 42px;
        }
        .user {
          &.reciever {
            color: #1441aa;
          }
          &.publisher {
            color: #f00;
          }
        }
      }
      .record-card {
        background-color: #faf9f9;
        padding: 82px;
        border-radius: 61px;
        .more-detail {
          margin: 42px 0;
          .card-imgs {
            margin-top: 42px;
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            div {
              position: relative;
              &::before {
                inset: 0;
                content: "";
                position: absolute;
                background-color: rgba(0, 0, 0, 0.1);
                z-index: 1;
              }
              img {
                width: 100%;
                height: 100%;
                object-fit: cover;
              }
            }
          }
        }
        .card-files,
        .card-links {
          a {
            color: #1441aa !important;
          }
        }
        .card-links {
          margin-top: 42px;
          display: flex;
        }
      }
      .btns {
        display: flex;
        justify-content: flex-end;
        margin-top: 82px;
        gap: 82px;
        .ant-btn {
          flex: 1;
          padding: 0 !important;
          &.ant-btn-dangerous {
            color: #ff4d4f !important;
          }
          &.ant-btn-primary:not(.ant-btn-dangerous) {
            background-color: #1441aa !important;
          }
        }
      }
    }
  }
}
main {
  font-size: 72px;
  padding: 82px;
  background-color: #f5f5f5;
  .inner {
    border-radius: 40px;
    .detail-item {
      display: flex;
      align-items: center;
      .label {
        flex-shrink: 0;
      }
      & > *:last-child {
        color: #f70202;
      }
    }
    .task-header {
      display: flex;
      gap: 40px;
      align-items: center;
      justify-content: space-between;
      .status {
        color: #0aa9a9;
        flex-shrink: 0;
      }
    }
    .title {
      // word-break: break-all;
      // text-overflow: ellipsis;
      // -webkit-line-clamp: 2;
      // -webkit-box-orient: vertical;
      // display: -webkit-box;
      // overflow: hidden;
      font-size: 82px;
      margin-bottom: 82px;
    }
    .raw-html {
      margin-top: 82px;
    }
  }
  .grid {
    display: flex;
    flex-wrap: wrap;
    & > * {
      width: 50%;
      margin-bottom: 40px;
      padding-right: 40px;
      &:nth-child(even) {
        padding-left: 40px;
        padding-right: 0;
      }
      &:last-child {
        width: 100%;
        margin-bottom: 0;
      }
    }
  }
}
</style>
